﻿import VTitle from '@/components/common/MainTitle.vue'
export default {
  data () {
    return {
      // 下拉选项应用编码
      appCodeList: [],
      // 已发布的版本信息
      showVersion: '暂无已发布版本信息',
      publishVersionList: [],
      // 禁止选择
      startDisabled: true,
      isDisabled: true,
      titleName: '流程管理',
      tableData: [],
      // 编辑和新增的弹窗部分
      dialogTitle: '',
      dialogFormVisible: false,
      // 表单规则
      ruleForm: {
        Title: '',
        Code: '',
        classify: '',
        Remark: '',
        AppCode: '',
        Status: '',
        CurWFID: ''
      },
      labelWidth: '94px',
      operationType: '',
      rules: {
        Code: [
          { required: true, message: '编码不能为空', trigger: 'blur' }
        ],
        AppCode: [
          { required: true, message: '应用编码不能为空', trigger: 'blur' }
        ],
        Title: [
          { required: true, message: '应用名称不能为空', trigger: 'blur' }
        ],
        Status: [
          { required: true, message: '应用状态不能为空', trigger: 'change' }
        ],
        classify: [
          { required: true, message: '类型不能为空', trigger: 'blur' }
        ],
        CurWFID: [
          { required: true, message: '版本信息不能为空', trigger: 'change' }
        ]
      },
      // 页码部分
      searchKey: '',
      totalLength: 0,
      currentPage: 1,
      pageSize: 10,
      loading: false
    }
  },
  created () {
    this.getTableData()
  },
  methods: {
    // 初始化下拉数据(应用信息列表)
    getAppCode () {
      this.appCodeList = []
      this.$basicWFPost('/WorkFlowMgr/Applications', {
        Query: {
          title: ''
        },
        limit: 10000,
        page: 1
      }).then((data) => {
        var codeinfo = data.data.result.code
        if (codeinfo === 'success') {
          this.appCodeList = data.data.result.data.QueryDatas
        }
      })
    },
    // 初始化表格的数据
    getTableData () {
      this.loading = true
      this.$basicWFPost('/WorkFlowMgr/WorkflowBases', {
        Query: {
          title: this.searchKey
        },
        limit: this.pageSize,
        page: this.currentPage
      }).then((data) => {
        var codeinfo = data.data.result.code
        if (codeinfo === 'success') {
          this.totalLength = data.data.result.data.TotalRow
          this.loading = false
          // 将后端数据拿到赋到table中
          this.tableData = data.data.result.data.QueryDatas
          // 遍历表格中的状态
          this.tableData.forEach((item, index) => {
            if (item.Status === 0) {
              item.Status = '停用'
            } else if (item.Status === 1) {
              item.Status = '启用'
            }
            if (item.Version === '') {
              item.Version = '未发布'
            }
          })
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    // 添加流程
    handleAdd () {
      // 添加引用时可以编辑
      this.showVersion = '当前无需选择版本'
      this.startDisabled = true
      this.isDisabled = false
      this.dialogFormVisible = true
      this.dialogTitle = '添加管理'
      this.ruleForm.Title = ''
      this.ruleForm.Code = ''
      this.ruleForm.classify = ''
      this.ruleForm.Remark = ''
      this.ruleForm.AppCode = ''
      this.ruleForm.Status = ''
      this.ruleForm.CurWFID = ''
      this.operationType = 'add'
    },
    // 流程定义
    handlePath (row) {
    // 跳转传入 Code请求接口
      this.$router.push({path: '/dashboard/workflowDefine', query: {Code: row.Code, CurWFID: row.CurWFID}})
    },
    // 编辑流程
    handleEdit (row) {
      this.startDisabled = false
      this.isDisabled = true
      this.dialogFormVisible = true
      this.dialogTitle = '编辑流程'
      this.ruleForm.Title = row.Title
      this.ruleForm.Code = row.Code
      this.ruleForm.classify = row.Type
      this.ruleForm.Remark = row.Remark
      this.ruleForm.AppCode = row.AppCode
      this.ruleForm.Status = row.Status
      this.ruleForm.CurWFID = row.CurWFID
      this.operationType = 'edit'
      // 调后端接口
      this.publishedVersion(row.Code)
    },
    // 根据编辑应用的Code获取当前流程的已发布的版本信息
    publishedVersion (publishedCode) {
      this.loading = true
      this.$basicWFPost('/WorkFlowMgr/GetWorkflowBaseMasters', {
        Code: publishedCode
      }).then((data) => {
        if (data.data.result.code === 'success') {
          this.loading = false
          // 存在data.data.result.data的情况
          if (data.data.result.data.length) {
            this.showVersion = '请选已发布版本信息'
            this.publishVersionList = data.data.result.data
          } else {
            this.showVersion = '暂无已发布版本信息'
            this.publishVersionList = data.data.result.data
          }
        } else {
          this.$message({
            message: data.data.result.msg,
            type: 'error'
          })
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    // 删除应用
    handleDelete (row) {
      this.$confirm('此操作将永久删除该流程, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonClass: 'el-button--info',
        cancelButtonText: '取消'
      }).then(() => {
        this.$basicWFPost('/WorkFlowMgr/DeleteWorkflowBase', {
          Code: row.Code
        }).then((data) => {
          let result = data.data.result
          if (result.code === 'success') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            // 表格添加成功后重新渲染页面
            this.getTableData()
          }
        }).catch((error) => {
          if (error.response.status === 404) {
            this.$message.error('删除失败，请重试！')
          }
        })
      }).catch(() => { })
    },
    // 提交编辑的方法
    submitEdit (options) {
      if (this.ruleForm.Status === '启用') {
        this.ruleForm.Status = '1'
      } else if (this.ruleForm.Status === '停用') {
        this.ruleForm.Status = '0'
      }
      this.$basicWFPost('/WorkFlowMgr/UpdateWorkflowBase', {
        Base: {
          Title: this.ruleForm.Title,
          Code: this.ruleForm.Code,
          Type: this.ruleForm.classify,
          Status: this.ruleForm.Status,
          Remark: this.ruleForm.Remark,
          AppCode: this.ruleForm.AppCode,
          CurWFID: this.ruleForm.CurWFID
        }
      }).then((data) => {
        let dataInfo = data.data.result
        if (dataInfo.code === 'success') {
          this.$message({
            message: dataInfo.msg,
            type: 'success'
          })
          // 成功后消除弹框
          this.dialogFormVisible = false
          // 表格添加成功后重新渲染页面
          this.getTableData()
        } if (dataInfo.code === 'failed') {
          this.$message({
            message: dataInfo.msg,
            type: 'error'
          })
        }
      })
    },
    // 提交新增的方法
    submitAdd (options) {
      this.$basicWFPost('/WorkFlowMgr/CreateWorkflowBase', {
        Base: {
          Title: this.ruleForm.Title,
          Code: this.ruleForm.Code,
          Type: this.ruleForm.classify,
          Status: this.ruleForm.Status,
          Remark: this.ruleForm.Remark,
          AppCode: this.ruleForm.AppCode,
          CurWFID: this.ruleForm.CurWFID
        }
      }).then((data) => {
        let dataInfo = data.data.result
        if (dataInfo.code === 'success') {
          this.$message({
            message: dataInfo.msg,
            type: 'success'
          })
          // 成功后消除弹框
          this.dialogFormVisible = false
          // 表格添加成功后重新渲染页面
          this.getTableData()
        } if (dataInfo.code === 'failed') {
          this.$message({
            message: dataInfo.msg,
            type: 'warning'
          })
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    // 点击弹窗的确定按钮
    submitForm (forms, submitType) {
      this.$refs[forms].validate((valid) => {
        if (valid) {
          if (submitType === 'edit') {
            this.submitEdit()
          } else if (submitType === 'add') {
            this.submitAdd()
          }
        } else {
          return false
        }
      })
    },
    // 点击弹窗的取消按钮
    resetForm (forms) {
      this.$refs[forms].clearValidate()
      this.dialogFormVisible = false
    },
    closeDialog () {
      this.$refs.ruleForm.clearValidate()
    },
    // 底部页码的部分
    handleSizeChange (val) {
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChange (val) {
      this.currentPage = val
      this.getTableData()
    },
    // 获取检索的值
    getSearchData () {
      this.currentPage = 1
      this.getTableData()
    }
  },
  components: {
    VTitle
  }
}
